//
//  LCustomAlertView.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/4.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LCustomAlertView.h"

@interface LCustomAlertView ()
/**  底层白色视图  */
@property (strong, nonatomic) UIView  *backView;
/**  提示语  */
@property (strong, nonatomic) UILabel  *alertLabel;
/**  提示语  */
@property (strong, nonatomic) NSString  *message;

@end
@implementation LCustomAlertView

- (instancetype)initWithFrame:(CGRect)frame alertMessage:(NSString *)message titlesArray:(NSArray *)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        self.message = message;
        [self layoutUI];
        CGFloat width  = ([UIScreen zj_screenWith] - 80 * [UIScreen zj_screenWScale]) / 2;
        for (int i = 0; i < titles.count; i ++) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(width * i, 80 * [UIScreen zj_screenHScale], width, 40 * [UIScreen zj_screenHScale])];
            [button setTitle:titles[i] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = i;
            button.titleLabel.font = [UIFont systemFontOfSize:17 * [UIScreen zj_textFontScale]];
            
            if (i == 1) {
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                button.backgroundColor = [UIColor themeColor];

            }else{
                [button setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
                button.backgroundColor = [UIColor whiteColor];

            }
            [self.backView addSubview:button];
            

        }
        
        UIView *center = [UIView new];
        center.backgroundColor = [UIColor themeGrayColor];
        [self.backView addSubview:center];
        center.sd_layout
        .leftEqualToView(self.backView)
        .rightEqualToView(self.backView)
        .heightIs(1)
        .topSpaceToView(self.backView,80 * [UIScreen zj_screenHScale]);
       

    }
    return self;
}



#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.backView];
    [self.backView addSubview:self.alertLabel];
   
    
    self.backView.sd_layout
    .leftSpaceToView(self,40 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self,40 * [UIScreen zj_screenWScale])
    .centerYEqualToView(self)
    .heightIs(120 * [UIScreen zj_screenHScale]);


    
    self.alertLabel.sd_layout
    .leftEqualToView(self.backView)
    .rightEqualToView(self.backView)
    .heightIs(20 * [UIScreen zj_screenHScale])
    .topSpaceToView(self.backView , 30 * [UIScreen zj_screenHScale]);
    
    

    
    
}


#pragma mark - 监听事件
- (void)clickButton:(UIButton *)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(lCustomAlertWithView:index:)]) {
        [self.delegate lCustomAlertWithView:self index:sender.tag];
    }
}


#pragma mark - getter


- (UILabel *)alertLabel{
    if (!_alertLabel) {
        _alertLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor textBlackColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont systemFontOfSize:18 * [UIScreen zj_textFontScale]];
            label.text = self.message;
            label;
        });
    }
    return _alertLabel;
}


- (UIView *)backView{
    if (!_backView) {
        _backView = [UIView new];
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.layer.cornerRadius = 4;
        _backView.layer.masksToBounds = YES;
    }
    return _backView;
}



@end
